package com.gc.baselib.utils;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;


import com.gc.baselib.core.CoreApi;

import java.util.UUID;

public class DeviceUtils {
    public static String getDeviceId(Context context) {
        /**
         * 得到全局唯一UUID
         */
        String uuid = "35" + //we make this look like a valid IMEI
                Build.BOARD.length() % 10 +
                Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 +
                Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 +
                Build.HOST.length() % 10 +
                Build.ID.length() % 10 +
                Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 +
                Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 +
                Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 digits
        SharedPreferences mShare = context.getSharedPreferences("uuid", MODE_PRIVATE);
        if (mShare != null) {
            uuid = mShare.getString("uuid", "");
        }
        if (TextUtils.isEmpty(uuid)) {
            uuid = UUID.randomUUID().toString();
            mShare.edit().putString("uuid", uuid).commit();
        }
        return uuid;
    }


    /**
     * 获取软件版本信息
     *
     * @return
     */
    public static PackageInfo getPackageInfo() {
        Context context = CoreApi.getInstance().getContext();
        PackageManager pm = context.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = pm.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packInfo;
    }

    public static String getMarket() {
        String channel = null;
        try {
            ApplicationInfo appInfo = CoreApi.getInstance().getContext().getPackageManager()
                    .getApplicationInfo(CoreApi.getInstance().getContext().getPackageName(), PackageManager.GET_META_DATA);
            channel = appInfo.metaData.getString("UMENG_CHANNEL");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(channel)) {
            channel = "baidu";
        }
        return channel;
    }

}
